from openpyxl import load_workbook
from data import data
from jinja2 import Environment, FileSystemLoader
from dict_to_lua import dict_to_lua

# 读取 Excel 数据
g_env = None

def read_excel(file_path):
    wb = load_workbook(file_path)
    sheet = wb.active

    data = []
    for row in sheet.iter_rows(min_row=4, values_only=True):  # 跳过标题行
        data.append(row)

    return data

def render_to_lua(data, template_file):
    global g_env
    g_env = Environment(loader=FileSystemLoader('templates'))
    template = g_env.get_template(template_file)

    lua_code = template.render(data=data, name='Talent')
    return lua_code

# 保存渲染后的 Lua 代码为文件
def save_lua_file(lua_code, output_path):
    with open(output_path, 'w', encoding='utf-8') as file:
        file.write(lua_code)


def main():
    # 读取 Excel 文件中的数据
    # excel_data = read_excel('excel/Talent.xlsx')

    # 渲染 Excel 数据为 Lua 表格
    # lua_code = render_to_lua(data, 'lua_table_template.j2')

    lua_code = dict_to_lua(data)
    lua_code = f"Talent = {lua_code}"

    # 将渲染后的 Lua 代码保存到文件
    save_lua_file(lua_code, 'Talent.lua')

    print("Lua file has been saved as 'Talent.lua'")


if __name__ == '__main__':
    main()
